package fnord.jovian.tango;

import java.awt.*;
import java.awt.geom.Rectangle2D;

public class TheArrow extends TheTriangle {
	
	protected Color _background, _out = new Color(150,150,150), _over = new Color(120,120,120), _click = new Color(40,40,40);
	protected TheListener _listener = null;

	public TheArrow() {
		init();
	}
	
	public TheArrow(double rot) {
		super(rot);
		init();
	}
	
	public TheArrow(double rot, TheListener listener) {
		super(rot);
		_listener = listener;
		init();
	}
	
	private void init() {
		_thePolygon = new Polygon(new int[] {0,5,10,12,12,28,28,30,35,40,20},new int[] {30,27,25,25,50,50,25,25,27,30,0},11);
		_background = _out;
	}
	
	public void paint(Graphics2D g) {
		Rectangle2D box = _thePolygon.getBounds2D();
		g.rotate(_rot,box.getCenterX(), box.getCenterY());
	    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g.setColor(_background);
		g.fill(_thePolygon);
	    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
		g.rotate(-_rot,box.getCenterX(), box.getCenterY());

	}
	
	public void setLocation(double x, double y) {
		Rectangle2D box = _thePolygon.getBounds2D();
		_thePolygon.translate((int)(x - box.getX() + 50), (int)(y - box.getY() + 30));
	}

	@Override
    public boolean contains(Point p) {
		if (_thePolygon.contains(p))
			containAction();
		else
			outAction();
	    return _thePolygon.contains(p);
    }

	@Override
    public boolean containsClick(Point p) {
		if (_thePolygon.contains(p))
			clickAction();
		else
			outAction();
	    return _thePolygon.contains(p);
    }

	@Override
    public boolean containsDrag(Point p) {
	    // TODO Auto-generated method stub
	    return false;
    }

	public void setTheListener(TheListener listener) {
		_listener = listener;
	}
	
	public boolean containsUp(Point p) {
		if (_thePolygon.contains(p))
			upAction();
		else
			outAction();
		return _thePolygon.contains(p);
	}
	
	public void containAction() {
		_background = _over;
	}
	
	public void clickAction() {
		_background = _click;
	}
	
	public void upAction() {
		_background = _out;
		if (_listener != null)
			_listener.doAction();
	}
	
	public void outAction() {
		_background = _out;
	}

	@Override
    public double getX() {
	    return _thePolygon.getBounds2D().getX() - 50;
    }

	@Override
    public double getY() {
	    return _thePolygon.getBounds2D().getY() - 30;
    }

	@Override
    public double getCenterX() {
	    return _thePolygon.getBounds2D().getCenterX();
    }

	@Override
    public double getCenterY() {
	    return _thePolygon.getBounds2D().getCenterY();
    }

	@Override
    public double getWidth() {
	    return _thePolygon.getBounds2D().getWidth() + 100;
    }

	@Override
    public double getHeight() {
	    return _thePolygon.getBounds2D().getHeight() + 60;
    }

	@Override
    public void setSize(double w, double h) {
	    // TODO Auto-generated method stub
	    
    }
}
